package db;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;

import controller.RoleController;
import db.sysuser;
import utils.IResult;
import utils.MD5Util;
import utils.TreeNode;
import utils.comutils;


public class admindb {
	public IResult addUser(String account, String name, String organization, String pass, String phone, String email,
			String state) {
		// 判断账号唯一
		List<sysuser> list = sysuser.dao.find("select * from sys_user where account=?",account);
		if(list.size()>0) {
			return IResult.fail("璐︽埛鍚嶉噸鍚嶏紝娣诲姞鐢ㄦ埛澶辫触锛�");
		}
		
		// 注册时间
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        
     // 生成一个8位随机字符串，保存到数据库，用于密码加密
        String veris;
        veris = comutils.getRandomString(8);
        
     // 使用用户注册日期作为md5密码加密的盐值
        String pwd = MD5Util.getMD5Code(pass + veris);
        
        sysuser itemuser = new sysuser();
        itemuser.set("account", account).set("name", name).set("organization", organization).set("pass", pwd).set("passver", veris).set("phone",phone).set("email",email).set("state",state).set("regtime", timestamp).save();

		return IResult.ok();
	}
	public IResult editUser(String id, String account, String name, String organization, String pass, String phone, String email,
			String state) {
		sysuser item = sysuser.dao.findById(id);
	
		// 如果输入修改密码为空，不修改密码
		if(pass == null) {
			item.set("account", account).set("name", name).set("organization", organization).set("phone",phone).set("email",email).set("state",state).update();
			return IResult.ok();
		}
		
		if(pass.length()<= 0) {
			item.set("account", account).set("name", name).set("organization", organization).set("phone",phone).set("email",email).set("state",state).update();
			return IResult.ok();
		}
		
		// 注册时间和密码校验不修改
		String passver = item.getStr("passver");
        String pwd = MD5Util.getMD5Code(pass + passver);
		item.set("account", account).set("name", name).set("organization", organization).set("pass", pwd).set("phone",phone).set("email",email).set("state",state).update();
		return IResult.ok();
	}
	public IResult delUser(String id) {
		sysuser.dao.deleteById(id);
	
		return IResult.ok();
	}
	public IResult getusers() {
		return IResult.ok(sysuser.dao.find("select id,name from sys_user"));
	}

	public IResult userlist() {
		List<sysuser> list = sysuser.dao.findAll();
		return IResult.ok(list);
	}

	 // 获取指定用户的名称
	public String getUserName(String id) {
		sysuser user = sysuser.dao.findById(id);
		return user.getStr("name");
	}
	 public static int update(String sql, Object... params) {
	        return Db.update(sql, params);
	    }
	// 更新用户密码
    public boolean updatePassword(String userId, String newPassword) {
        try {
            // 获取当前用户
            sysuser user = sysuser.dao.findById(userId);
            if (user == null) {
                return false;
            }
            
            // 使用当前用户的passver来加密新密码
            String passver = user.getStr("passver");
            String hashedPassword = MD5Util.getMD5Code(newPassword + passver);

            // 更新密码
            user.set("pass", hashedPassword).update();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
